package tds.model22;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Constructor;

/**
 * @Company 杭州先锋电子技术股份有限公司
 * @ClassName Client
 * @Description TODO（这里用一句话描述这个类的作用)
 * @author tds
 * @Date 2020/2/5 22:40
 * @version 1.0.0
 */
public class Client2 {
    public static void main(String[] args) throws Exception {
        // 通过反射破解单例模式（枚举除外）
        Class<Singleton1> clazz = Singleton1.class;
        Constructor<Singleton1> constructor = clazz.getDeclaredConstructor();
        constructor.setAccessible(true);
        Singleton1 singleton1 = constructor.newInstance();
        Singleton1 singleton2 = constructor.newInstance();
        System.out.println(singleton1 == singleton2);

        // singleton6通过私有构造方法里面判断防止反射破解
        Class<Singleton6> clazz2 = Singleton6.class;
        Constructor<Singleton6> constructor2 = clazz2.getDeclaredConstructor();
        constructor2.setAccessible(true);
        Singleton6 singleton6 = constructor2.newInstance();
        Singleton6 singleton7 = constructor2.newInstance();
        System.out.println(singleton6 == singleton7);



    }
}
